package org.honeyweb.gsoc.pages;

import java.util.ArrayList;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.InjectComponent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.Service;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.corelib.components.Zone;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.honeyweb.gsoc.model.Honeypots;
import org.honeyweb.gsoc.model.Organizations;
import org.honeyweb.gsoc.model.Users;
import org.honeyweb.gsoc.services.HoneypotsManager;
import org.honeyweb.gsoc.services.OrganizationsManager;
import org.honeyweb.gsoc.services.UsersManager;

@SuppressWarnings("unused")
public class ManageHoneypot {
	
	private final Log logger = LogFactory.getLog(CreateOrganization.class);
	
	@Inject
	@Service("honeypotsManager")
	private HoneypotsManager honeypotsManager;
	
	@Inject
	@Service("organizationsManager")
	private OrganizationsManager organizationsManager;
	
	@Inject
	@Service("usersManager")
	private UsersManager usersManager;
	
	@Property
	@InjectComponent
	private Zone honeypotsZone;
	
	@SessionState(create=true)
	private Users user;

	@Component
	private Form form;
	
	
	@Property
	private String name;
	
	@Property
	private String country;
	
	@Property
	private Honeypots honeypotLoop;
	
	@Property
	private String url;
	
	public ArrayList<Honeypots> getHoneypots() {
		int userId = user.getIdUser();
		Organizations organization = organizationsManager.getOrganizationByAdminId(userId);
		logger.debug("Organization name:"+organization.getName());
		return honeypotsManager.getHoneypots(organization);
	}
	
	String onSuccess(){
		int userId = user.getIdUser();
		Honeypots honeypot = honeypotsManager.addHoneypot(name,url,country);
		Organizations organization = organizationsManager.getOrganizationByAdminId(userId);
		organizationsManager.addOrganizationsHoneypotsId(organization.getId(),honeypot.getId());
		return "manageHoneypot";
	};
	
    Object onActionFromShowHoneypotsZone()
    {
		return honeypotsZone.getBody();
    }
    
    Object onActionFromRemove(int honeypotId)
    {
    	int userId = user.getIdUser();
    	Honeypots honeypot = honeypotsManager.delHoneypot(honeypotId);
    	Organizations organization = organizationsManager.getOrganizationByAdminId(userId);
    	organizationsManager.delOrganizationsHoneypotsId(organization.getId(),honeypot.getId());
    	return "manageHoneypot";
    }

}
